package com.example.lemonhealth.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.lemonhealth.constant.MessageConstant;
import com.example.lemonhealth.entity.TCheckitem;
import com.example.lemonhealth.service.CheckitemService;
import com.example.lemonhealth.utils.PageResult;
import com.example.lemonhealth.utils.QueryPageBean;
import com.example.lemonhealth.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @Author: Lemon
 * @Description: 检查项管理相关接口
 * @Date: 2024/12/24 10:51
 */
@Controller
@RequestMapping("/checkitem")
@Slf4j
public class CheckitemController {

    @Resource
    private CheckitemService checkitemService;

    /**
     * @Author Lemon
     * @Description 分页查询-检查项管理
     * @Date 2024/12/24 11:12
     * @Param [queryPageBean]
     * @Return com.example.lemonhealth.utils.PageResult
     */
    @PostMapping("/findPage.do")
    @ResponseBody
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        IPage<TCheckitem> page = checkitemService.findPage(queryPageBean);
        return new PageResult(page.getTotal(), page.getRecords());
    }

    /**
     * @Author Lemon
     * @Description 根据id查询检查项（数据回显）
     * @Date 2024/12/24 14:26
     * @Param [id]
     * @Return com.example.lemonhealth.utils.Result
     */
    @GetMapping("/findById.do")
    @ResponseBody
    public Result findById(@RequestParam("id") Integer id) {
        boolean flag;
        String message;
        Object data;
        try {
            TCheckitem tCheckitem = checkitemService.findById(id);
            if (!Objects.isNull(tCheckitem)) {
                flag = true;
                message = MessageConstant.QUERY_CHECKITEM_SUCCESS;
                data = tCheckitem;
            } else {
                flag = false;
                message = MessageConstant.QUERY_CHECKITEM_FAIL;
                data = "";
            }

        } catch (Exception e) {
            flag = false;
            message = MessageConstant.QUERY_CHECKITEM_FAIL;
            data = e.getMessage();
            e.printStackTrace();
        }
        return new Result(flag, message, data);
    }

    /**
     * @Author Lemon
     * @Description 添加检查项
     * @Date 2024/12/25 14:10
     * @Param [tCheckitem]
     * @Return com.example.lemonhealth.utils.Result
     */
    @PostMapping("add.do")
    @ResponseBody
    public Result addCheckitem(@RequestBody TCheckitem tCheckitem) {
        boolean flag;
        String message;
        Object data;
        try {
            if (StringUtils.isNotEmpty(tCheckitem.getCode())&&StringUtils.isNotEmpty(tCheckitem.getName())) {
                checkitemService.addCheckitem(tCheckitem);
                flag = true;
                message = MessageConstant.ADD_CHECKITEM_SUCCESS;
                data = "";
            } else {
                flag = false;
                message = MessageConstant.ADD_CHECKITEM_FAIL;
                data = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
            message = MessageConstant.ADD_CHECKITEM_FAIL;
            data = null;
        }
        return new Result(flag, message, data);
    }

    /**
     * @Author Lemon
     * @Description 删除检查项
     * @Date 2024/12/25 14:32
     * @Param [id]
     * @Return com.example.lemonhealth.utils.Result
     */
    @GetMapping("delete.do")
    @ResponseBody
    public Result deleteCheckitem(@RequestParam("id") Integer id) {
        boolean flag;
        String message;
        Object data;
        try{
            checkitemService.deleteCheckitem(id);
            flag = true;
            message = MessageConstant.DELETE_CHECKITEM_SUCCESS;
            data = null;
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
            message = MessageConstant.DELETE_CHECKITEM_FAIL;
            data = "";
        }
        return new Result(flag, message, data);
    }

    /**
     * @Author Lemon
     * @Description 保存修改-检查项管理
     * @Date 2024/12/24 15:33
     * @Param [tCheckitem]
     * @Return com.example.lemonhealth.utils.Result
     */
    @PostMapping("/edit.do")
    @ResponseBody
    public Result edit(@RequestBody TCheckitem tCheckitem) {
        boolean flag;
        String message;
        Object data;
        try {
            checkitemService.edit(tCheckitem);
            flag = true;
            message = MessageConstant.EDIT_CHECKITEM_SUCCESS;
            data = null;
        } catch (Exception e) {
            flag = false;
            message = MessageConstant.EDIT_CHECKITEM_FAIL;
            data = e.getMessage();
        }
        return new Result(flag, message, data);
    }

    /**
     * @Author Lemon
     * @Description 查询所有检查项
     * @Date 2024/12/24 17:43
     * @Param []
     * @Return com.example.lemonhealth.utils.Result
     */
    @GetMapping("findAll.do")
    @ResponseBody
    public Result findAll() {
        boolean flag;
        String message;
        Object data;
        try {
            flag = true;
            message = MessageConstant.QUERY_CHECKITEM_SUCCESS;
            data = checkitemService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
            message = MessageConstant.QUERY_CHECKITEM_FAIL;
            data = e.getMessage();
        }
        return new Result(flag, message, data);
    }


}
